package logic

import (
	"context"
	"edu-pro/models/mysql"
	"time"

	"edu-pro/edurpc/edurpc"
	"edu-pro/edurpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type EvaluationListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewEvaluationListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EvaluationListLogic {
	return &EvaluationListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *EvaluationListLogic) EvaluationList(in *edurpc.EvaluationListRequest) (*edurpc.EvaluationListResponse, error) {
	// 评论列表
	var lists []mysql.EvalData
	mysql.DB.Debug().Model(&mysql.Evaluation{}).
		Select("users.nickname,evaluations.*").
		Joins("left join users on users.id = evaluations.user_id").
		Order("created_at desc").
		Scopes(mysql.Paginate(int(in.Page), int(in.Size))).
		Find(&lists)
	var listData []*edurpc.EvaluationList
	for _, list := range lists {
		evaluationList := edurpc.EvaluationList{
			Nickname: list.Nickname,
			Score:    int64(list.Score),
			Content:  list.Content,
			CreateAt: list.CreatedAt.Format(time.DateTime),
		}
		listData = append(listData, &evaluationList)
	}
	return &edurpc.EvaluationListResponse{
		List: listData,
	}, nil
}
